package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.SysStudentInteraction;
import com.ruoyi.system.mapper.SysStudentInteractionMapper;
import com.ruoyi.system.service.SysStudentInteractionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 学生课堂互动记录Service实现类
 *
 * @author ruoyi
 * @date 2025-08-29
 */
@Service
public class SysStudentInteractionServiceImpl implements SysStudentInteractionService
{
    @Autowired
    private SysStudentInteractionMapper interactionMapper;

    @Override
    public List<SysStudentInteraction> getInteractionsByUserId(Long userId) {
        validateUserId(userId);
        return interactionMapper.selectByUserId(userId);
    }

    @Override
    public List<SysStudentInteraction> getInteractionsByUserIdAndTimeRange(
            Long userId, Date startTime, Date endTime) {
        validateUserId(userId);
        if (startTime == null || endTime == null) {
            throw new ServiceException("时间范围不能为空");
        }
        if (startTime.after(endTime)) {
            throw new ServiceException("开始时间不能晚于结束时间");
        }
        return interactionMapper.selectByUserIdAndTimeRange(userId, startTime, endTime);
    }

    @Override
    public List<SysStudentInteraction> getInteractionsByUserIdAndCourseId(
            Long userId, Long courseId) {
        validateUserId(userId);
        if (courseId == null || courseId <= 0) {
            throw new ServiceException("课程ID不合法");
        }
        return interactionMapper.selectByUserIdAndCourseId(userId, courseId);
    }

    @Override
    public Long countTotalInteractions(Long userId) {
        validateUserId(userId);
        return interactionMapper.countTotalByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> countInteractionsByType(Long userId) {
        validateUserId(userId);
        return interactionMapper.countByTypeAndUserId(userId);
    }

    @Override
    @Transactional
    public boolean addInteraction(SysStudentInteraction interaction) {
        if (interaction.getUserId() == null || interaction.getUserId() <= 0) {
            throw new ServiceException("用户ID不合法");
        }
        if (interaction.getCourseId() == null || interaction.getCourseId() <= 0) {
            throw new ServiceException("课程ID不合法");
        }
        if (interaction.getInteractionType() == null
                || interaction.getInteractionType() < 1
                || interaction.getInteractionType() > 5) {
            throw new ServiceException("互动类型不合法");
        }
        if (interaction.getInteractionTime() == null) {
            interaction.setInteractionTime(new Date());
        }
        interaction.setStatus(1);
        return interactionMapper.insert(interaction) > 0;
    }

    /**
     * 验证用户ID合法性
     */
    private void validateUserId(Long userId) {
        if (userId == null || userId <= 0) {
            throw new ServiceException("用户ID不合法");
        }
    }
}
